import axios from 'axios'; // 使用 axios 作为 HTTP 客户端

const http = axios.create({
   baseURL: 'http://192.168.123.203:8080',
  // baseURL:'http://192.168.101.143:8080',
});

// 格式化get请求url参数，将对象解析为字符串
function urlFormater(url, params) {
  if (params) {
    let paramList = [];
    for (let key in params) {
      paramList.push(key + '=' + encodeURIComponent(params[key]));
    }
    return url.indexOf('?') > -1 ? (url + '&' + paramList.join('&')) : (url + '?' + paramList.join('&'));
  }
  return url;
}

// 混入默认个性化配置
function mixinCustom(config) {
  config.custom = Object.assign({
    // auth: true,
    toast: true,
    catch: true,
    loading: true,
  }, config.custom || {});
  return config;
}

const request = {
  // post提交
  post(url, params, config = {}) {
    // config = mixinCustom(config);
    return http.post(url, params, config);
  },
  // get提交
  get(url, params, config = {}) {
    config = mixinCustom(config);
    let path = urlFormater(url, params);
    return http.get(path, config);
  },
  // put提交
  put(url, params, config = {}) {
    // config = mixinCustom(config);
    return http.put(url, params, config);
  },
  // delete提交
  delete(url, params, config = {}) {
    // config = mixinCustom(config);
    return http.delete(url, { data: params, ...config });
  },
};

export default request;